package view.Core;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.*;

public class TabActivityC extends Activity {
	String treeID;
	String longitude, latitude;

	Button mapbutton1, mapbutton2;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.select_map);

		Bundle bunde = this.getIntent().getExtras();
		treeID = bunde.getString("treeIDForMap");

		findViewByID();
		selectFromlongitudeAndlatitude(treeID);

	}

	public void viewNormalMap(View view) {
		broweMap("normal");
	}

	public void viewStreetMap(View view) {
		broweMap("street");
	}

	public void broweMap(String map) {
		if (map.equals("normal")) {
			// 22.3830641 114.2008729
			Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("geo:"
					+ longitude + "," + latitude + "?z=17"));
			startActivity(intent);
		} else if (map.equals("street")) {
			Intent intent = new Intent(
					Intent.ACTION_VIEW,
					Uri.parse("google.streetview:cbll="+ longitude + ","+latitude+"&cbp=1,200,,0,1"));
			startActivity(intent);
		} else
			toastMsg("null");

	}

	public void selectFromlongitudeAndlatitude(String treeID) {
		// TODO Auto-generated method stub
		String reply;
		try {
			// String url =
			// "http://192.168.1.101/oldTreeWeb/getJsonField.php?Name=Tree tat";

			String url = getResources().getString(R.string.serverURL)
					+ "getJsonByTreeID.php?inputTreeID=" + treeID;
			// getJsonByTreeID.php
			HttpClient client = new DefaultHttpClient();
			HttpGet request = new HttpGet(url);
			HttpResponse response = client.execute(request);

			String line = "";
			reply = "";
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					response.getEntity().getContent()));
			while ((line = reader.readLine()) != null) {
				reply = line;
			}
			reader.close();
			try {

				JSONArray json = new JSONArray(reply);

				// toastMsg(""+json.toString());
				JSONArray jsonArrayB = new JSONArray(json.getString(0));
				String getField = jsonArrayB.getString(0);

				JSONObject jsonField = new JSONObject(getField);

				latitude = jsonField.getString("latitude");
				longitude = jsonField.getString("longitude");

			} catch (JSONException e) {
				toastMsg("" + e);
			}
		} catch (Exception e) {
			toastMsg("" + e);
		}
	}

	public void findViewByID() {
		mapbutton1 = (Button) findViewById(R.id.mapbutton1);
		mapbutton2 = (Button) findViewById(R.id.mapbutton2);
	}

	public void toastMsg(String msg) {
		Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
	}
}